package howl.types.query;

import howl.types.DataProvider;

import java.util.ArrayList;
import java.util.Date;
import java.util.Hashtable;

import flexjson.JSON;

public class Result implements Comparable {

	private Date timestamp;
	
	private Hashtable<String, item> map;
	
	private DataProvider dp;

	public Result(Date timestamp) {
		super();
		this.timestamp = timestamp;
		this.map = new Hashtable<String, item>();
	}

	public void put(String key, item value) {
		this.map.put(key, value);
	}

	public item getValue(String name) {
		return this.map.get(name);
	}

	@Override
	public String toString() {
		return "Result [map=" + map + ", timestamp=" + timestamp + "]";
	}

	public String toXML() {
		String ret = "<Entry SensorName=\""+dp.getName()+"\" Longitude=\""+dp.getLongitude()+"\" Latitude=\""+dp.getLatitude()+"\" timestamp=\"" + this.getTimestamp().toString() +"\">";

		ArrayList e = new ArrayList(map.values());

		for (int i = 0; i < e.size(); i++) {
			item a = (item) e.get(i);
			ret = ret + a.toXML();
		}
		ret = ret + "</Entry>";

		return ret;

	}

	@Override
	public int compareTo(Object o) {
		Result s = (Result) o;
		return s.timestamp.compareTo(this.timestamp);
	}

	public Date getTimestamp() {
		return timestamp;
	}

	public Hashtable<String, item> getMap() {
		return map;
	}

	public void setMap(Hashtable<String, item> map) {
		this.map = map;
	}

	/**
	 * @return the dp
	 */
	public DataProvider getDp() {
		return dp;
	}

	/**
	 * @param dp the dp to set
	 */
	public void setDp(DataProvider dp) {
		this.dp = dp;
	}

}